<?php

class DotTableRenderer {

	function GetTableName( $xTable ) {
		if ( is_object( $xTable ) ) {
			return $xTable->GetName();
		}
		return $xTable;
	}
	
	function Render( &$oConfiguration, &$xTable ) {

		$sTableName = DotTableRenderer::GetTableName( $xTable );
		$oTableRegister =& RenderedTableRegister::GetInstance();
		if ( $oTableRegister->WasTableRendered( $sTableName ) ) {
			return '';
		}
		$oTableRegister->SetTableRendered( $sTableName );

		$sTableUrlProperty = DotTableRenderer::GetTableUrlProperty( $oConfiguration, $xTable );


		if ( $oConfiguration->GetProperty( 'ShowColumns' ) && is_object( $xTable ) ) {
			return $sTableName . ' [shape=record'. $sTableUrlProperty .', label="'.DotTableRenderer::RenderColumns( $oConfiguration, $xTable ).'"]' . "\n";
		} else {
			return $sTableName . ' [shape=box'. $sTableUrlProperty .']' . "\n";
		}
	}
	
	function GetTableUrlProperty( &$oConfiguration, &$xTable ) {
		
		$sTableName = DotTableRenderer::GetTableName( $xTable );
		$sBaseUrl = $oConfiguration->GetProperty( 'BaseTableLinkUrl' );

		if ( $sBaseUrl ) {
			return ', URL="' . $sBaseUrl . $sTableName . '" ';	
		}
		return '';
	}
	
	function RenderColumns( &$oConfiguration, &$oTable ) {
		
		$sColumnPropertiesList = implode( '|', array_merge( array( '' )                , $oTable->GetColumnProperties()     ) ); 
		$sColumnNameList       = implode( '|', array_merge( array( $oTable->GetName() ), $oTable->GetColumnNames()     ) ); 
		$sColumnDataTypes      = implode( '|', array_merge( array( '' )                , $oTable->GetColumnDataTypes() ) );

		if ( $oConfiguration->GetProperty( 'ShowColumnProperties' ) ) {
			return '{' . $sColumnPropertiesList . '} | {' . $sColumnNameList . '} | {' . $sColumnDataTypes . '}';
		} else {
			return '{' . $sColumnNameList . '} | {' . $sColumnDataTypes . '}';
		}
	}
}

?>
